package com.ruoyi.info.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.info.mapper.CmsContentMapper;
import com.ruoyi.info.domain.CmsContent;
import com.ruoyi.info.service.ICmsContentService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.ui.ModelMap;

/**
 * 新闻Service业务层处理
 *
 * @author xwj
 * @date 2023-07-07
 */
@Service
public class CmsContentServiceImpl implements ICmsContentService {
    @Autowired
    private CmsContentMapper cmsContentMapper;

    /**
     * 查询新闻
     *
     * @param id 新闻主键
     * @return 新闻
     */
    @Override
    public CmsContent selectCmsContentById(Long id) {
        return cmsContentMapper.selectCmsContentById(id);
    }

    /**
     * 查询新闻列表
     *
     * @param cmsContent 新闻
     * @return 新闻
     */
    @Override
    public List<CmsContent> selectCmsContentList(CmsContent cmsContent) {
        return cmsContentMapper.selectCmsContentList(cmsContent);
    }

    /**
     * 新增新闻
     *
     * @param cmsContent 新闻
     * @return 结果
     */
    @Override
    public int insertCmsContent(CmsContent cmsContent) {
        cmsContent.setCreateTime(DateUtils.getNowDate());
        return cmsContentMapper.insertCmsContent(cmsContent);
    }

    /**
     * 修改新闻
     *
     * @param cmsContent 新闻
     * @return 结果
     */
    @Override
    public int updateCmsContent(CmsContent cmsContent) {
        cmsContent.setUpdateTime(DateUtils.getNowDate());
        return cmsContentMapper.updateCmsContent(cmsContent);
    }

    /**
     * 批量删除新闻
     *
     * @param ids 需要删除的新闻主键
     * @return 结果
     */
    @Override
    public int deleteCmsContentByIds(String ids) {
        return cmsContentMapper.deleteCmsContentByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除新闻信息
     *
     * @param id 新闻主键
     * @return 结果
     */
    @Override
    public int deleteCmsContentById(Long id) {
        return cmsContentMapper.deleteCmsContentById(id);
    }

    @Override
    public List<CmsContent> getCmsContent(String kind) {
        List<CmsContent> list = Lists.newArrayList();

        CmsContent param = new CmsContent();
        param.setKind(kind);
        list = cmsContentMapper.selectCmsContentList(param);
        return list;
    }

    @Override
    public List<CmsContent> getCmsContentByKind(String kind, String hotFlag, int limit) {
        List<CmsContent> list = Lists.newArrayList();
        CmsContent param = new CmsContent();
        param.setKind(kind);
        param.setHotFlag(hotFlag);
        param.setLimit(limit);
        list = cmsContentMapper.selectCmsContentListLimit(param);
        return list;
    }

    @Override
    public void setViewAdd(CmsContent cmsContent) {
        Long num = cmsContent.getViewNum();

        if (num == null) {
            num = 0L;
        }

        num++;
        cmsContent.setViewNum(num);
        updateCmsContent(cmsContent);
    }

    @Override
    public List<Long> getLikes(Long contnetId) {
        return cmsContentMapper.getLikes(contnetId);
    }

    @Override
    public void setcontentLike(Long contentId, Long userId) {
        Map param = new HashMap();
        param.put("userId", userId);
        param.put("contentId", contentId);

        Long id = cmsContentMapper.getUserContentLike(param);
        System.out.println(">>>>>>>>>>>>>>>>" + id);

        if (id != null) {
            cmsContentMapper.removeLike(id);
        } else {
            cmsContentMapper.addLike(param);
        }
    }

    @Override
    public List<CmsContent> search(String seo) {
        List<CmsContent> contents = cmsContentMapper.search(seo);
        return contents;
    }


    @Override
    public void indexNews(ModelMap mmap) {
        List<CmsContent> list = Lists.newArrayList();
        CmsContent param = new CmsContent();
        param.setKind("NEWS");
        param.setStatus("1");

        list = cmsContentMapper.selectCmsContentList(param);

        List<CmsContent> thumbs = Lists.newArrayList();
        List<CmsContent> news = Lists.newArrayList();
        List<Long> existIds = Lists.newArrayList();

        int max = 5;
        int flag = 0;
        CmsContent temp = null;
        for (int i = 0; i < list.size(); i++) {
            temp = list.get(i);
            if(flag >= max) {
                break;
            }
            if(!StringUtils.isEmpty(temp.getThumb())) {
                existIds.add(temp.getId());
                thumbs.add(temp);
                flag++;
            }
        }

        flag = 0;
        for (int i = 0; i < list.size(); i++) {
            temp = list.get(i);
            if(flag >= max) {
                break;
            }

            if(existIds.indexOf(temp.getId()) < 0) {
                news.add(temp);
                flag ++;
            }
        }
        System.out.println("xxxxxxxxxxxxx" + thumbs.size());
        mmap.put("thumbs", thumbs);
        mmap.put("news", news);

    }

    @Override
    public CmsContent selectCmsContentByUuId(String uuid) {
        return cmsContentMapper.selectCmsContentByUuId(uuid);
    }
}
